package com.dingding.special.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dingding.common.core.base.controller.BaseController;
import com.dingding.common.core.base.dto.FacadeResult;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.facade.special.trasfer.calc_price.ReqCalculationAmountDTO;
import com.dingding.facade.special.trasfer.today_info.ReqGetTodayInfo;
import com.dingding.facade.special.trasfer.today_info.ResGetTodayInfo;
import com.dingding.order.common.dto.distance.ReqRealTimeCalculationDTO;
import com.dingding.order.common.dto.distance.ResRealTimeCalculationDTO;
import com.dingding.order.dto.phonecall.ReqGetPhoneCall;
import com.dingding.order.dto.phonecall.ResGetPhoneCall;
import com.dingding.order.facade.common.distance.ICalculationFacade;
import com.dingding.order.service.IUserOrderFacade;
import com.dingding.order.service.special.ISpecialCarOrderFacade;
import com.dingding.special.order.aboard.biz.IConfirmAboardService;
import com.dingding.special.order.aboard.dto.ReqConfirmAboardDTO;
import com.dingding.special.order.aboard.dto.ResConfirmAboardDTO;
import com.dingding.special.order.bill.biz.WaterBillService;
import com.dingding.special.order.bill.dto.ReqWaterBillDTO;
import com.dingding.special.order.bill.dto.ResWaterBillDTO;
import com.dingding.special.order.calculation.service.ICalculationAmountService;
import com.dingding.special.order.cancel.biz.ICancelSpecialOrderService;
import com.dingding.special.order.cancel.dto.ReqCancelSpecialOrderDTO;
import com.dingding.special.order.cancel.dto.ResCancelSpecialOrderDTO;
import com.dingding.special.order.confirm.biz.IConfirmArriveService;
import com.dingding.special.order.confirm.dto.ReqConfirmArriveDTO;
import com.dingding.special.order.confirm.dto.ResConfirmArriveDTO;
import com.dingding.special.order.grab.dto.ReqUserGrabDTO;
import com.dingding.special.order.grab.dto.ResUserGrabDTO;
import com.dingding.special.order.grab.service.IUserGrabService;
import com.dingding.special.order.route.dto.ReqGetMapShowRoute;
import com.dingding.special.order.route.service.IRecommendedRouteService;
import com.dingding.special.user.driver.service.IDriverUserService;
import com.dingding.special.user.setDepositPwd.dto.DepositPasswordVerifyDTO;
import com.dingding.special.user.setDepositPwd.dto.ReqDepositPwdDTO;
import com.dingding.special.user.setDepositPwd.dto.TellPhoneValidateDTO;
import com.dingding.special.user.setDepositPwd.service.DepositPwdService;
import com.dingding.special.user.validate.service.IValidateCodeService;

@Controller
@RequestMapping("/authorapi/special/driver/order")
public class SpecialOrderController extends BaseController {

	Logger logger = Logger.getLogger(SpecialOrderController.class);

	// 账单流水service
	@Resource(name = "waterBillService")
	private WaterBillService waterBillService;

	// 设置提现密码
	@Resource(name = "depositPwdService")
	DepositPwdService depositPwdService;

	@Resource(name = "validateCodeService")
	IValidateCodeService iValidateCodeService;

	@Resource(name = "specialCarOrderFacade")
	private ISpecialCarOrderFacade specialCarOrderFacade;

	@Resource(name = "driverUserService")
	private IDriverUserService driverUserService;

	@Resource(name = "confirmAboardService")
	private IConfirmAboardService confirmAboardService;

	@Resource(name = "confirmArriveService")
	private IConfirmArriveService confirmArriveService;

	@Resource(name = "userGrabService")
	private IUserGrabService userGrabService;

	@Resource(name = "cancelSpecialOrderService")
	private ICancelSpecialOrderService cancelSpecialOrderService;

	@Resource(name = "calculationAmountService")
	private ICalculationAmountService calculationAmountService;

	@Resource(name = "recommendedRouteService")
	private IRecommendedRouteService recommendedRouteService;
	
	@Resource(name = "userOrderFacade")
	private IUserOrderFacade userOrderFacade;

	@Resource(name = "calculationFacade")
	private ICalculationFacade calculationFacade;
	
	/**
	 * @Description:获取司机端进行中订单
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/makeOrder", method = { RequestMethod.POST })
	public void makeOrder(HttpServletRequest request, HttpServletResponse response) {

		String clientData = super.getClientData(request, response);
		String responseData = this.specialCarOrderFacade.getDriverUnderWayOrders(clientData);
		super.responseData(response, responseData);
	}

	/**
	 * @Description:获取司机首页信息
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/driverTodayInfo", method = { RequestMethod.POST })
	public void driverTodayInfo(HttpServletRequest request, HttpServletResponse response) {

		ReqGetTodayInfo getTodayInfo = super.getRequestBean(request, response, ReqGetTodayInfo.class);
		ResGetTodayInfo resGetTodayInfo = null;
		
		// 判断客户端请求参数是否为空
		if(getTodayInfo != null){
			
			// 如果用户角色3,则为出租车,否则为专车
			if(getTodayInfo.getUserRole() == null || getTodayInfo.getUserRole() == 1 || getTodayInfo.getUserRole() == 2){
				resGetTodayInfo = this.driverUserService.getDriverTodayInfo(getTodayInfo);
			}else if(getTodayInfo.getUserRole() == 3){
				resGetTodayInfo = this.driverUserService.getTaxiDriverTodayInfo(getTodayInfo);
			}
		}
		
		if (resGetTodayInfo == null) {
			super.generateNullParamRes(response);
		} else {
			super.generateSuccessResponse(response, new ResponseEntity<ReqGetTodayInfo>(), resGetTodayInfo);
		}
	}

	/**
	 * @Description:获取司机历史订单
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/historicalOrder", method = { RequestMethod.POST })
	public void historicalOrder(HttpServletRequest request, HttpServletResponse response) {
		String clientData = super.getClientData(request, response);
		String responseData = this.specialCarOrderFacade.getDriverHistoryOrders(clientData);
		super.responseData(response, responseData);
	}

	/**
	 * @Description:获取司机历史订单
	 * @author YHQ
	 * @since 2.5.5
	 */
	@RequestMapping(value = "/v1/completeAndCancelOrder", method = { RequestMethod.POST })
	public void completeAndCancelOrder(HttpServletRequest request, HttpServletResponse response){
		
		String clientData = super.getClientData(request, response);
		String responseData = this.specialCarOrderFacade.completeAndCancelOrder(clientData);
		super.responseData(response, responseData);
	}
	
	/**
	 * @Description:确认上车
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/confirmInCar", method = { RequestMethod.POST })
	public void getInCar(HttpServletRequest request, HttpServletResponse response) {
		ReqConfirmAboardDTO reqUserDepositDetailDTO = super.getRequestBean(request, response,
				ReqConfirmAboardDTO.class);
		ResponseEntity<ResConfirmAboardDTO> responseEntity = confirmAboardService
				.confirmAboard(reqUserDepositDetailDTO);
		this.responseData(response, responseEntity);
	}

	/**
	 * @Description:确认到达
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/confirmArrivial", method = { RequestMethod.POST })
	public void confirmArrivial(HttpServletRequest request, HttpServletResponse response) {
		ReqConfirmArriveDTO reqConfirmArriveDTO = super.getRequestBean(request, response, ReqConfirmArriveDTO.class);
		ResponseEntity<ResConfirmArriveDTO> responseEntity = confirmArriveService.confirmArrive(reqConfirmArriveDTO);
		this.responseData(response, responseEntity);
	}

	/**
	 * @Description:专车抢单（抢预约单）
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/grabOrder", method = { RequestMethod.POST })
	public void grabOrder(HttpServletRequest request, HttpServletResponse response) {

		ReqUserGrabDTO userGrabDTO = super.getRequestBean(request, response, ReqUserGrabDTO.class);
		ResponseEntity<ResUserGrabDTO> responseEntity = userGrabService.userGrab(userGrabDTO);
		this.responseData(response, responseEntity);
	}

	/**
	 * @Description:司机取消订单
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/driverCancelOrder", method = { RequestMethod.POST })
	public void driverCancelOrder(HttpServletRequest request, HttpServletResponse response) {

		ReqCancelSpecialOrderDTO cancelSpecialOrderDTO = super.getRequestBean(request, response,
				ReqCancelSpecialOrderDTO.class);
		ResponseEntity<ResCancelSpecialOrderDTO> responseEntity = cancelSpecialOrderService
				.cancelSpecialOrder(cancelSpecialOrderDTO);
		this.responseData(response, responseEntity);
	}

	/**
	 * 
	 * @Title: tradingInquiry
	 * @Description: 专车 - 流水查询
	 * @param
	 * @return void
	 * @author SUNZHICHENG
	 * @date 2016年8月25日 下午3:13:11
	 * @since v2.4.0
	 */
	@RequestMapping(value = "/v1/tradingInquiry", method = { RequestMethod.POST })
	public void tradingInquiry(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端请求，将请求数据转换成请求bean
		ReqWaterBillDTO reqWaterBillDTO = super.getRequestBean(request, response, ReqWaterBillDTO.class);
		// 调取service层的业务方法
		ResponseEntity<ResWaterBillDTO> resWaterBillDTO = null;
		
		if(reqWaterBillDTO != null){
			
			if(reqWaterBillDTO.getUserRole() == null || reqWaterBillDTO.getUserRole() == 1 || reqWaterBillDTO.getUserRole() == 2){
				resWaterBillDTO = waterBillService.queryWaterBill(reqWaterBillDTO);
			}else{
				resWaterBillDTO = waterBillService.queryTaxiWaterBill(reqWaterBillDTO);
			}
		}
		
		// 响应客户端请求
		super.responseData(response, resWaterBillDTO);

	}

	/**
	 * 
	 * @Title: getSpecialTellPhoneValidate
	 * @Description: 专车 - 手机验证码校验
	 * @return void
	 * @author SUNZHICHENG
	 * @date 2016年8月26日 上午9:27:52
	 * @since v2.4.0
	 */
	@RequestMapping(value = "/v1/getSpecialTellPhoneValidate", method = { RequestMethod.POST })
	public void getSpecialTellPhoneValidate(HttpServletRequest request, HttpServletResponse response) {
		// 获取客户端请求，将请求数据转换成请求bean
		TellPhoneValidateDTO tellPhoneValidate = super.getRequestBean(request, response, TellPhoneValidateDTO.class);
		// 调用service方法处理并响应客户端
		super.responseData(response, depositPwdService.getSpecialTellPhoneValidate(tellPhoneValidate));

	}

	@RequestMapping(value = "/v1/orderPreference", method = { RequestMethod.POST })
	public void orderPreference(HttpServletRequest request, HttpServletResponse response) {

	}

	@RequestMapping(value = "/v1/getTellPhoneValidate", method = { RequestMethod.POST })
	public void getTellPhoneValidate(HttpServletRequest request, HttpServletResponse response) {
	}

	/**
	 * 
	 * @Title: orderFlowDetail
	 * @Description: 专车 - 订单流水详情
	 * @param
	 * @return void
	 * @author SUNZHICHENG
	 * @date 2016年8月25日 下午3:42:32
	 * @since v2.4.0
	 */
	@RequestMapping(value = "/v1/orderFlowDetail", method = { RequestMethod.POST })
	public void orderFlowDetail(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端请求，将请求数据转换成请求bean
		ReqWaterBillDTO reqWaterBillDTO = super.getRequestBean(request, response, ReqWaterBillDTO.class);

		if(reqWaterBillDTO != null){
			
			if(reqWaterBillDTO.getUserRole() == null || reqWaterBillDTO.getUserRole() == 1 || reqWaterBillDTO.getUserRole() == 2){
				// 调用service方法处理并响应客户端（专车）
				super.responseData(response, waterBillService.queryOrderParticulars(reqWaterBillDTO));
			}else{
				// 调用service方法处理并响应客户端(出租车)
				super.responseData(response, waterBillService.queryTaxiOrderParticulars(reqWaterBillDTO));
			}
		}
	}

	/**
	 * 
	 * @Title: completionNumberDetail
	 * @Description: 专车 - 订单完成数详情
	 * @param
	 * @return void
	 * @author SUNZHICHENG
	 * @date 2016年8月25日 下午3:29:14
	 * @since v2.4.0
	 */
	@RequestMapping(value = "/v1/completionNumberDetail", method = { RequestMethod.POST })
	public void completionNumberDetail(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端请求，将请求数据转换成请求bean
		ReqWaterBillDTO reqWaterBillDTO = super.getRequestBean(request, response, ReqWaterBillDTO.class);

		if(reqWaterBillDTO != null){
			
			if(reqWaterBillDTO.getUserRole() == null || reqWaterBillDTO.getUserRole() == 1 || reqWaterBillDTO.getUserRole() == 2){
				// 调用service方法处理并响应客户端(专车)
				super.responseData(response, waterBillService.queryOrderFinishAmount(reqWaterBillDTO));
			}else{
				// 调用service方法处理并响应客户端(出租车)
				super.responseData(response, waterBillService.queryTaxiOrderFinishAmount(reqWaterBillDTO));
			}
		}
	}

	/**
	 * 
	 * @Title: completionRateDetail
	 * @Description: 专车 - 订单完成率详情
	 * @param
	 * @return void
	 * @author SUNZHICHENG
	 * @date 2016年8月25日 下午3:39:37
	 * @since v2.4.0
	 * @see 修改hufan
	 */
	@RequestMapping(value = "/v1/completionRateDetail", method = { RequestMethod.POST })
	public void completionRateDetail(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端请求，将请求数据转换成请求bean
		ReqWaterBillDTO reqWaterBillDTO = super.getRequestBean(request, response, ReqWaterBillDTO.class);

		if(reqWaterBillDTO != null){
			
			if(reqWaterBillDTO.getUserRole() == null || reqWaterBillDTO.getUserRole() == 1 || reqWaterBillDTO.getUserRole() == 2){
				// 调用service方法处理并响应客户端
				super.responseData(response, waterBillService.queryOrderFinishRate(reqWaterBillDTO));
			}else{
				// 调用service方法处理并响应客户端
				super.responseData(response, waterBillService.queryTaxiOrderFinishRate(reqWaterBillDTO));
			}
		}
	}

	/**
	 * 
	 * @Title: setDepositPassword
	 * @Description: 专车 - 设置提现密码
	 * @param
	 * @return void
	 * @author SUNZHICHENG
	 * @date 2016年8月26日 上午10:59:10
	 * @since v2.4.0
	 */
	@RequestMapping(value = "/v1/setDepositPassword", method = { RequestMethod.POST })
	public void setDepositPassword(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端请求，将请求数据转换成请求bean
		ReqDepositPwdDTO reqDepositPwdDTO = super.getRequestBean(request, response, ReqDepositPwdDTO.class);

		// 调用service方法处理并响应客户端
		super.responseData(response, depositPwdService.setDepositPwd(reqDepositPwdDTO));

	}

	/**
	 * 
	 * @Title: DepositPasswordVerify
	 * @Description: 提现密码校验
	 * @param request
	 * @param response
	 * @return void
	 * @author SUNZHICHENG
	 * @date 2016年8月29日 下午7:45:00
	 * @since v
	 */
	@RequestMapping(value = "/v1/depositPasswordVerify", method = { RequestMethod.POST })
	public void DepositPasswordVerify(HttpServletRequest request, HttpServletResponse response) {
		DepositPasswordVerifyDTO depositPasswordVerifyDTO = super.getRequestBean(request, response,
				DepositPasswordVerifyDTO.class);
		super.responseData(response, depositPwdService.DepositPasswordVerify(depositPasswordVerifyDTO));

	}

	/**
	 * 实时计算价格
	 * 
	 * @Title: calculationAmountService
	 * @author CHEHAUNBO
	 * 
	 * 
	 */
	@Deprecated
	@RequestMapping(value = "/v1/calculationAmount", method = RequestMethod.POST)
	public void calculationAmount(HttpServletRequest request, HttpServletResponse response) {

		ReqCalculationAmountDTO reqCalculationAmountDTO = super.getRequestBean(request, response,
				ReqCalculationAmountDTO.class);
		this.responseData(response, calculationAmountService.calculationAmount(reqCalculationAmountDTO));

	}

	/**
	 * @Description:获取推荐路线
	 * @author CHEHUANBO
	 * @since 2.4
	 * 
	 */
	@RequestMapping(value = "/v1/getRecommendRoute", method = RequestMethod.POST)
	public void getRecommendRoute(HttpServletRequest request, HttpServletResponse response) {

		ReqGetMapShowRoute reqGetMapShowRoute = super.getRequestBean(request, response, ReqGetMapShowRoute.class);
		this.responseData(response, recommendedRouteService.getRecommendRouteByPosition(reqGetMapShowRoute));
	}

	/**
	 * @Description:获取拨打的电话号码
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/getPhoneCall", method = RequestMethod.POST)
	public void getPhoneCall(HttpServletRequest request, HttpServletResponse response) {

		ReqGetPhoneCall reqGetPhoneCall = super.getRequestBean(request, response, ReqGetPhoneCall.class);

		// 验证参数是否为空
		if (ValidateUtil.validateBean(reqGetPhoneCall, ReqGetPhoneCall.class) == false) {
			super.generateNullParamRes(response);
		}

		FacadeResult<ResGetPhoneCall> phoneCall = this.userOrderFacade.getPhoneCall(reqGetPhoneCall);

		super.generateFacadeResponse(response, new ResponseEntity<ResGetPhoneCall>(), phoneCall);
	}
	
	/**
	 * 
	 * @Title: realCalculation 
	 * @Description: 实时计算
	 * @author YHQ 
	 * @since V2.5.7
	 * 
	 */
	@RequestMapping(value = "/v1/realCalculation", method = RequestMethod.POST)
	public void realCalculation(HttpServletRequest request, HttpServletResponse response){
		
		// 客户端请求参数
		ReqRealTimeCalculationDTO reqRealTimeCalculationDTO = super.getRequestBean(request, response, ReqRealTimeCalculationDTO.class);
		
		// 获取实时计算距离价格响应Bean
		ResRealTimeCalculationDTO resRealTimeCalculationDTO = calculationFacade.realTimeCalculation(reqRealTimeCalculationDTO);
		
		// 响应Bean
		FacadeResult<ResRealTimeCalculationDTO> realTimeCalculation = new FacadeResult<ResRealTimeCalculationDTO>();
		
		realTimeCalculation.setObject(resRealTimeCalculationDTO);
		
		super.generateFacadeResponse(response, new ResponseEntity<ResRealTimeCalculationDTO>(), realTimeCalculation);
	}
	
}
